<?php
//管理端后台信息展示

namespace App\Services\Manage;

use App\Repositories\DinnerLogRepository;
use App\Repositories\RestaurantRepository;
use App\Services\ActivityService;
use App\Services\CouponService;
use App\Services\ReceiverService;
use Carbon\Carbon;

class HomeManageService
{

    /**
     * 首页信息展示
     * @return array[]
     */
    public static function index() {
        $staInfo = self::indexStatistic();

        return [
            'statistic_info' => $staInfo,
        ];
    }

    /**
     * 首页信息-统计
     * @return array
     */
    public static function indexStatistic() {
        $output = [
            'activity_total' => 0, //活动数量
            'receiver_total' => 0, //受助人数量
            'restaurant_total' => 0, //助餐点数量
            'coupon_total' => 0, //发券数
            'coupon_consume_total' => 0, //券核销数
            'dinner_log_chart' => [], //用餐记录折线图
            'coupon_bar_graph' => [], //券核销发放柱状图
        ];
        //活动数量
        $output['activity_total'] = ActivityService::getTotal([
            'status' => 2,
        ]);
        //受助人数量
        $output['receiver_total'] = ReceiverService::getTotal();
        //助餐点数量
        $restaurantRepo = new RestaurantRepository();
        $restaurantRepo->buildQuery([
            'auth_status' => 50,
        ]);
        $output['restaurant_total'] = $restaurantRepo->getTotal();
        //发券数
        $output['coupon_total'] = CouponService::getTotal();
        //券核销数
        $output['coupon_consume_total'] = CouponService::getTotal([
            'consume_status' => 2,
        ]);
        //近七日统计数据
        //获取七天前时间
        $startDate = Carbon::now()->subDays(6)->format("Y-m-d 00:00:00");
        $endDate = Carbon::now()->toDateTimeString();
        //获取日期列表，升序
        $dateList = [];
        for ($i = 0; $i < 7; $i++) {
            $dateList[] = Carbon::parse($startDate)->addDays($i)->format("Y-m-d");
        }
        //用餐记录折线图
        $dinnerRepo = new DinnerLogRepository();
        $dinnerRepo->buildQuery([
            'created_at_gte' => $startDate,
            'created_at_lte' => $endDate,
            'select_raw' => "COUNT(1) num, date_format(created_at,'%Y-%m-%d') day",
            'group_field' => "day",
        ]);
        $dinnerLogChart = $dinnerRepo
            ->getList()
            ->toArray();
        $dinnerLogChart = shiftValToKey($dinnerLogChart, 'day');
        //拼装用餐记录折线图日期数据
        foreach($dateList as $date) {
            $output['dinner_log_chart'][] = [
                'date' => $date,
                'num' => $dinnerLogChart[$date]['num'] ?? 0,
            ];
        }
        //券核销发放柱状图
        //每日领券数
        $couponDayList = CouponService::getList([
            'receive_date_gte' => $startDate,
            'receive_date_lt' => $endDate,
            'select_raw' => "COUNT(1) num, date_format(receive_date,'%Y-%m-%d') day",
            'group_field' => "day",
        ]);
        $couponDayList = shiftValToKey($couponDayList, 'day');
        //每日核销数
        $consumeCouponDayList = CouponService::getList([
            'consume_date_gte' => $startDate,
            'consume_date_lt' => $endDate,
            'consume_status' => 2,
            'select_raw' => "COUNT(1) num, date_format(consume_date,'%Y-%m-%d') day",
            'group_field' => "day",
        ]);
        $consumeCouponDayList = shiftValToKey($consumeCouponDayList, 'day');

        foreach($dateList as $date) {
            $output['coupon_bar_graph'][] = [
                'date' => $date,
                'num' => $couponDayList[$date]['num'] ?? 0,
                'consume_num' => $consumeCouponDayList[$date]['num'] ?? 0,
            ];
        }

        return $output;
    }

}